package com.cxyd.modules.wms.controller;

import com.cxyd.common.page.PageData;
import com.cxyd.common.utils.Result;
import com.cxyd.common.annotation.LogOperation;
import com.cxyd.common.validator.AssertUtils;
import com.cxyd.common.validator.ValidatorUtils;
import com.cxyd.common.validator.group.AddGroup;
import com.cxyd.common.validator.group.DefaultGroup;
import com.cxyd.common.validator.group.UpdateGroup;
import com.cxyd.modules.security.user.SecurityUser;
import com.cxyd.modules.security.user.UserDetail;
import com.cxyd.modules.wms.dto.LocateDTO;
import com.cxyd.modules.wms.dto.request.BatchForbidRequest;
import com.cxyd.modules.wms.service.LocateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 货位信息
 *
 * @author g_Cycle gengruyi01@163.com
 * @since 1.0.0 2023-04-27
 */
@RestController
@RequestMapping("wms/locate")
public class LocateController {
    private final LocateService locateService;

    public LocateController(LocateService locateService) {
        this.locateService = locateService;
    }

    @GetMapping("page")
    @RequiresPermissions("wms:locate:page")
    public Result<PageData<LocateDTO>> page(@RequestParam Map<String, Object> params){
        PageData<LocateDTO> page = locateService.page(params);

        return new Result<PageData<LocateDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wms:locate:info")
    public Result<LocateDTO> get(@PathVariable("id") Long id){
        LocateDTO data = locateService.get(id);

        return new Result<LocateDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("wms:locate:save")
    public Result save(@RequestBody LocateDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        locateService.save(dto);

        return new Result();
    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("wms:locate:update")
    public Result update(@RequestBody LocateDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        locateService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @LogOperation("删除")
    @RequiresPermissions("wms:locate:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        locateService.delete(ids);

        return new Result();
    }

    @PostMapping("forbid/{id}")
    @LogOperation("禁用")
    public Result freeze(@PathVariable("id") Long locateId){
        UserDetail user = SecurityUser.getUser();
        //效验数据
        final Map<String,Object> map =new HashMap<>();
        map.put("locateId",locateId);
        map.put("userId",user.getId());
        locateService.locateDisabled(map);
        return new Result().ok("修改成功");
    }

    @PostMapping("/disables")
    @LogOperation("一键禁用(解禁)")
    public ResponseEntity<?> disable(@RequestBody BatchForbidRequest request) {
        List<Long> ids = request.getIds(); // 获取前端传递的 ID 集合
        Integer status = request.getStatus();
        if (ids == null || ids.isEmpty()) {
            return ResponseEntity.badRequest().body("ID 集合不能为空");
        }
        try {
            // 1. 执行业务逻辑（禁用/解禁）
            locateService.locateDisables(ids,status);
            // 2. 返回成功响应
            Map<String, Object> response = new HashMap<>();
            response.put("code", 0);
            response.put("msg", "操作成功");
            return ResponseEntity.ok(response);

        } catch (Exception e) {
            // 3. 返回失败响应
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("msg", "操作失败：" + e.getMessage());
            return ResponseEntity.status(500).body(errorResponse);
        }
    }
}